package com.example.datapedestal.toolChain.service.impl;

import com.example.datapedestal.toolChain.service.ToolChainService;
import com.example.datapedestal.toolChain.service.ToolPlugin;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;

/**
 * @author: ThinkPad
 * @since: 2023/11/4 13:00
 * @description:
 */
@Service
public class ToolChainImpl implements ToolChainService {

    private final AtomicReference<ToolPlugin> currentPlugin = new AtomicReference<>();

    private List<ToolPlugin> plugins = new ArrayList<>();

    private static ToolChainImpl toolChain;

    private ToolChainImpl(){}

    public static ToolChainImpl GetInstance()
    {
        if(toolChain==null)
        {
            toolChain=new ToolChainImpl();
        }

        return toolChain;
    }

    @Override
    public void SetCurrentPlugin(ToolPlugin plugin) {
        this.currentPlugin.set(plugin);
    }

    @Override
    public ToolPlugin GetCurrentPlugin() {
        return currentPlugin.get();
    }

    @Override
    public void Clear(){
        this.plugins.clear();
    }

    @Override
    public void SetTools(List<ToolPlugin> toolList){
        this.plugins = toolList;
    }

    @Override
    public void AppendTool(ToolPlugin plugin){
        this.plugins.add(plugin);
    }

    @Override
    public void RemoveTool(ToolPlugin plugin) {
        this.plugins.remove(plugin);
    }

    @Override
    public void ExecutePlugins() {
        for (ToolPlugin plugin : this.plugins) {
            SetCurrentPlugin(plugin);
            plugin.execute();
            SetCurrentPlugin(null);
        }
    }
}
